package com.hoshiicloud.common.validate.annotations.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.hoshiicloud.common.validate.annotations.Telephone;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 固定电话号码验证接口实现类
 *
 * @author : yangcheng
 * @date : 2019/5/13 14:08
 * @mail : maiboyc@163.com
 **/
@Service
public class TelephoneValidatorImpl implements ConstraintValidator<Telephone, String> {


    @Override
    public void initialize(Telephone constraintAnnotation) {

    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if(StringUtils.isBlank(value)){
            return false;
        }
        // 国内电话号码(0755-8805222、021-87888822)
        String regex = "\\d{3}-\\d{8}|\\d{4}-\\d{7}";
        if (value.matches(regex)) {
            return true;
        } else {
            return false;
        }
    }

}